package com.sj.uabrowser.mvp.main.presenter;

import com.sj.uabrowser.mvp.main.bean.News;
import com.sj.uabrowser.mvp.main.bean.PageBean;
import com.sj.uabrowser.mvp.main.contract.NewsContract;
import com.sj.uabrowser.mvp.main.loader.NewsLoader;

import java.util.List;
import java.util.TreeMap;

public class NewsPresenter implements NewsContract.Presenter {

    private NewsContract.View mView;
    private NewsLoader newsLoader;

    public NewsPresenter(NewsContract.View mView) {
        newsLoader = new NewsLoader(this);
        this.mView = mView;
    }

    @Override
    public void fetchNews(TreeMap<String, Object> params) {
        if(newsLoader!=null)newsLoader.fetchNewsData(0,params);
    }

    @Override
    public void onDataCallBack(int requestId, Object data) {
        if(mView==null)return;
        switch (requestId){
            case 0:
                PageBean<News> newsPageBean = (PageBean<News>) data;
                mView.showNews(newsPageBean.getData());
                break;
        }
    }

    @Override
    public void onDataCallBackError(int requestId, Throwable throwable) {
        throwable.printStackTrace();
        if(mView!=null)mView.showErrors();
    }

    @Override
    public void onDestroy() {
        if(mView!=null)mView=null;
        if(newsLoader!=null)newsLoader=null;
    }
}
